package com.zyb.warehouse.service.impl;

import com.zyb.warehouse.common.Result;
import com.zyb.warehouse.dto.AssignAuthDto;
import com.zyb.warehouse.dto.Page;
import com.zyb.warehouse.entity.Auth;
import com.zyb.warehouse.entity.CurrentUser;
import com.zyb.warehouse.entity.Role;
import com.zyb.warehouse.entity.RoleAuth;
import com.zyb.warehouse.mapper.RoleAuthMapper;
import com.zyb.warehouse.mapper.RoleMapper;
import com.zyb.warehouse.service.RoleService;
import com.zyb.warehouse.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Description:角色业务逻辑实现
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleAuthMapper roleAuthMapper;
    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 查询所有角色信息
     *
     * @return
     */
    public List<Role> queryRoleList() {
        return roleMapper.selectAllRole();
    }

    /**
     * 条件查询带分页
     *
     * @param page
     * @param role
     * @return
     */
    public Page pageQuery(Page page, Role role) {
        //获取总记录数
        Integer count = roleMapper.selectRoleCount(role);
        //分页查询
        List<Role> roleList = roleMapper.pageSelect(page, role);
        //添加进去
        page.setTotalNum(count);
        page.setResultList(roleList);
        return page;
    }

    /**
     * 添加角色
     *
     * @param role
     * @param token
     * @return
     */
    @Transactional
    public Result addRole(Role role, String token) {
        //判断角色账号或者角色名称是否存在
        Role r = roleMapper.selectRoleByRoleNameOrRoleCode(role.getRoleName(), role.getRoleCode());
        //角色已存在
        if (r != null) {
            return Result.err(Result.CODE_ERR_BUSINESS, "角色已存在");
        }
        //获取添加人的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        role.setCreateBy(currentUser.getUserId());
        //角色不存在进行添加
        Integer i = roleMapper.insertRole(role);
        if (i > 0) {
            return Result.ok("角色添加成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "角色添加失败");
    }

    /**
     * 角色的启用或禁用
     *
     * @param role
     * @param token
     * @return
     */
    @Transactional
    public Result modifyRoleState(Role role, String token) {
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //设置属性
        role.setUpdateBy(currentUser.getUserId());
        role.setUpdateTime(new Date());
        //修改状态
        Integer i = roleMapper.updateRole(role);
        if (i > 0) {
            return Result.ok("启用禁用成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "启用禁用失败");
    }

    /**
     * 修改角色
     *
     * @param role
     * @param token
     * @return
     */
    @Transactional
    public Result modifyRole(Role role, String token) {
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //设置属性
        role.setUpdateBy(currentUser.getUserId());
        role.setUpdateTime(new Date());
        //进行修改
        Integer i = roleMapper.updateRole(role);
        if (i > 0) {
            return Result.ok("修改成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "修改失败");
    }

    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    @Transactional
    public Result deleteRole(Integer roleId) {
        //先删除角色
        Integer i = roleMapper.deleteRole(roleId);
        if (i > 0) {
            //再删除角色对应的权限
            roleAuthMapper.deleteRoleAuth(roleId);
            return Result.ok("角色删除成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "角色删除失败");
    }

    /**
     * 根据角色ID查询已经分配给角色的权限
     *
     * @param roleId
     * @return
     */
    public List<Integer> queryAllAuthByRoleId(Integer roleId) {
        return roleAuthMapper.selectAllAuthByRoleId(roleId);
    }

    /**
     * 给角色分配权限（添加角色权限）
     *
     * @param assignAuthDto
     * @return
     */
    @Transactional
    public void addRoleAuth(AssignAuthDto assignAuthDto) {
        //先删除角色已有的权限
        roleAuthMapper.deleteRoleAuth(assignAuthDto.getRoleId());
        //取出权限Id
        List<Integer> authIds = assignAuthDto.getAuthIds();
        //一条一条添加
        for (Integer authId : authIds) {
            RoleAuth roleAuth = new RoleAuth();
            roleAuth.setRoleId(assignAuthDto.getRoleId());
            roleAuth.setAuthId(authId);
            roleAuthMapper.insertRoleAuth(roleAuth);
        }
    }
}
